<?php
class EnterActionController
{
	private $view;
	private $args;
	
	public function setView(View $view) { 
		$this->view = $view; 
		return $this;
	} 
	//вызывается из фабрики
	public function setArgs($args = null) { 
		if(null != $args) { 
			$this->args = $args;
		}
		return $this->args; 
	} 
	public function run() {
			if ($_SERVER['REQUEST_METHOD'] == 'POST') {
				$this->post();
			}
			if (!empty($this->args['get']['exit'])) {
				unset($_SESSION['user']);
				header('Location: index.php');
			}
			$this->view->setRenderTemplate("enter.php"); 
	}
	public function post() {
		if(!empty($this->args['post']['enter'])) {
			$mapperAutors = new MapperAutors();
			if (empty($this->args['post']['name']) OR empty($this->args['post']['pass'])) {
				$error = 'Одно из полей не заполнено!';
				$this->view->setError($error);		
			} else {
				$autor = $mapperAutors->findAutor($this->args['post']['name'], $this->args['post']['pass']);
				if ($autor) {
					$sid = serialize($autor);
					$_SESSION['user'] = $sid;
					header('Location: index.php');
				} else {
					$error = 'Нет пользователя с таким именем и(или) паролем!';
					$this->view->setError($error);	
				}
			}
		}
               
		$this->view->setRenderTemplate("enter.php"); 
	}
}
